# -*- codeing = utf-8 -*-
from pyecharts import options as opts
from pyecharts.charts import Bar, Timeline,Line,Page
import pandas as pd

df = pd.read_csv(r'gdp.csv')
province = ['北京市','山东省','广东省','河南省','广西','甘肃省']
df1 = df.loc[[0,14,18,15,19,27],:]
years = df1.iloc[0,1:21].index.values.tolist()
years = list(reversed(years))
yearss = []
for i in range(1997,2017):
     yearss.append(i)
gdp_year = []
for year in years:
    gdp_year.append(df1[year].values.tolist())
datas = []
for i in range(20):
    datas.append([yearss[i],gdp_year[i]])


def Timeline_bar():
    tl = Timeline()
    for data in datas:
        x_data = [n for n in province]
        y_data = [n for n in data[1]]
        x_y_data = {}
        for i in range(0, len(x_data)):
            x_y_data[x_data[i]] = y_data[i]
        x_y_data_sort = sorted(x_y_data.items(), key=lambda x: x[1], reverse=True)
        xaxis_data = []
        yaxis_data = []
        for i in range(0, len(x_y_data_sort)):
            xaxis_data.append(x_y_data_sort[i][0])
            yaxis_data.append(x_y_data_sort[i][1])
        bar = (
            Bar()
                .add_xaxis(xaxis_data)
                .add_yaxis("gdp", yaxis_data, label_opts=opts.LabelOpts(position="right"))
                .reversal_axis()
                .set_global_opts(title_opts=opts.TitleOpts("四省{}gdp".format(data[0])))
        )
        tl.add(bar, "{}年".format(data[0]))
        tl.add_schema(
            orient="vertical",
            play_interval=1100,  # 播放时间间隔；
            is_auto_play=True,
            is_inverse=True,
            pos_left="null",
            pos_right="5",
            pos_top="20",
            pos_bottom="20",
            width="50",
            control_position='left',  # 控制位置;
        )
    return tl
Timeline=Timeline_bar()
Timeline.render('考试C2.html')